package com.luoying.itew.http.rxjava;

import com.luoying.itew.http.BaseResponse;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * Created by lufan on 2019/4/10
 * Class describe
 */
public abstract class BaseObserver<T> implements Observer<BaseResponse<T>> {
    private static final int HANDLE_ERROR_CODE = 90000;
    private Disposable mDisposable;

    @Override
    public void onSubscribe(Disposable d) {
        mDisposable = d;
    }

    @Override
    public void onNext(BaseResponse<T> baseResponse) {
        if(baseResponse==null){
            onHandleError(HANDLE_ERROR_CODE,"处理错误");
            return;
        }
        if(baseResponse.isOk()){
            onSuccess(baseResponse.getResultEntity());
        } else {
            onHandleError(baseResponse.getResultCode(),baseResponse.getResultMessage());
        }



    }

    @Override
    public void onError(Throwable e) {
        onHandleError(HANDLE_ERROR_CODE,e.getMessage());
    }

    @Override
    public void onComplete() {

    }

    public abstract void onSuccess(T t);

    public void onHandleError(int code, String message) {

    }

    public Disposable getmDisposable() {
        return mDisposable;
    }
}
